
uniform sampler2D buffer;
uniform sampler2D normalBuffer;

uniform vec4 ambient;
uniform vec4 specular;
//uniform vec4 diffuse;
//uniform vec4 ligthDir;
uniform float shininess;

//vec4 ambient = vec4(0.3, 0.3, 0.3, 1.0);
//vec4 specular = vec4(1.0, 1.0, 1.0, 1.0);
//float shininess = 128.0;

void main (void) {    
    vec4 color = texture2D (buffer, gl_TexCoord[0].st);        
    if (color.a < 1.0) discard;
    
    vec3 normal = texture2D (normalBuffer, gl_TexCoord[0].st).xyz;
    normal = 2.0 * normal - 1.0;    
    if (normal == vec3(0.0, 0.0, 0.0) || normal == vec3(1.0, 1.0, 1.0)) {
        normal = vec3(0.0, 0.0, 1.0);
    }
    normal = normalize(normal);    
    
    vec3 lightDir = normalize(vec3(gl_LightSource[0].position));
    vec4 diffuse = color;
     
    color = ambient * (gl_LightSource[0].ambient + gl_LightModel.ambient);

    float NdotL = max(dot(normal, lightDir), 0.0);

    if (NdotL > 0.0) {
        color += diffuse * gl_LightSource[0].diffuse * NdotL;
        float NdotHV = max(dot(normal, gl_LightSource[0].halfVector.xyz), 0.0);
        color += specular * gl_LightSource[0].specular * pow(NdotHV, shininess);
    }

    gl_FragData[0] = color;
}
